package com.itheima.spring.bestpractive.controller.admin;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.spring.bestpractive.common.myResult.R;
import com.itheima.spring.bestpractive.pojo.AssessmentIndicator;
import com.itheima.spring.bestpractive.pojo.AssessmentPlan;
import com.itheima.spring.bestpractive.pojo.Department;
import com.itheima.spring.bestpractive.service.AssessmentService;
import com.itheima.spring.bestpractive.service.DepartmentService;
import com.itheima.spring.bestpractive.service.EmpService;
import com.itheima.spring.bestpractive.vo.req.assessment.AssessmentPlanReqVo;
import com.itheima.spring.bestpractive.vo.req.employee.EmpReqAdminVo;
import com.itheima.spring.bestpractive.vo.req.employee.EmpUpdataVo;
import com.itheima.spring.bestpractive.vo.resp.employee.EmpRespVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @version 1.0
 * @author: HZY
 * @date: 2025/6/25 17:13
 * @Content:
 */
@RestController
@RequestMapping("/admin")

public class AdminController {
    @Autowired
    private AssessmentService assessmentService;

    @Autowired
    private EmpService empService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ObjectMapper objectMapper;

    @Operation(description = "添加考核计划，并配置考核指标")
    @PostMapping("/assessment/publish")
    public R publishAssessment(@RequestBody @Valid AssessmentPlanReqVo planVo) {
        AssessmentPlan plan = new AssessmentPlan();
        System.out.println("planVo" + planVo.toString());
        List<AssessmentIndicator> indicators = planVo.getIndicators().stream()
                .map(vo -> {
                    var indicator = new AssessmentIndicator(); // 使用 var 简化变量声明
                    BeanUtils.copyProperties(vo, indicator);
                    return indicator;
                }).toList();
        BeanUtils.copyProperties(planVo, plan);
        assessmentService.publishAssessment(plan, indicators);
        return R.success();
    }

    @Operation(description = "添加员工")
    @PostMapping("/emp/empAdd")
    public R empAdd(@RequestParam("jsonData") String empVo, @RequestParam("photo") MultipartFile photo) throws JsonProcessingException {
        EmpReqAdminVo empReqAdminVo = objectMapper.readValue(empVo, EmpReqAdminVo.class);
        empService.empAdd(empReqAdminVo, photo);
        return R.success();
    }

    @Operation(description = "删除考核计划")
    @DeleteMapping("/assessment/delete")
    public R deleteAssessmentPlan(@RequestBody @Valid List<Long> planIds) {
        assessmentService.deleteAssessmentPlan(planIds);
        return R.success();
    }

    @Operation(description = "获取部门考核计划")
    @GetMapping("/assessment/getPlan")
    public R getPlan(@RequestParam int pageNum
            , @RequestParam int pageSize) {
        PageInfo<AssessmentPlan> plan = assessmentService.getPlan(pageNum, pageSize);
        return R.success(plan);
    }

    @Operation(description = "添加部门")
    @PostMapping("/department/add")
    public R addDepartment(@RequestBody List<Department> department) {
        departmentService.addDepartment(department);
        return R.success();
    }

    @Operation(description = "获取部门")
    @GetMapping("/department/getAll")
    public R getAllDepartment() {
        return R.success(departmentService.getAllDepartment());
    }

    @Operation(description = "获取所有员工信息")
    @GetMapping("/emp/getAll")
    public R getAllEmp(@RequestParam int pageNum,
                       @RequestParam int pageSize) {
        List<EmpRespVo> allEmp = empService.getAllEmp(pageNum, pageSize);
        return R.success(allEmp);
    }

    @Operation(description = "修改员工信息")
    @PutMapping("/emp/update")
    public R updateEmp(@RequestParam("jsonData") String empVo, @RequestParam(name = "photo", required = false) MultipartFile photo) throws JsonProcessingException {
        EmpUpdataVo empUpdataVo = objectMapper.readValue(empVo, EmpUpdataVo.class);
        empService.updateEmp(empUpdataVo, photo);
        return R.success();
    }
}

